package com.safe.app.config;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Spring 根据线程标识获取 当前请求的session web request 项目中使用
 * @author h2l
 */

public final class ContextHolderUtils {

	private ContextHolderUtils() {
		super();
	}

	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	public static HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}

	public static HttpSession getSession() {
		HttpSession session = getRequest().getSession();
		return session;

	}

	public static HttpSession getSession(boolean create) {
		HttpSession session = getRequest().getSession(create);
		return session;

	}
}
